<?php
class signModel extends appModel{
	/**
	 *获得一条微信record记录
	 */
	public function get_record($id)
	{
	
		if(!$id)
		{
			return false;
		}
		$sql = "SELECT *,nickname as name FROM ".$this->table('wxusers')." WHERE wxid =".$id;
		$result=$this->getRow($sql);
	
		if($result)
		{
	
			return $result;
		}
		else
		{
			return false;
		}
	}
	public function insert_record($wxid){
		$sql=" SELECT id FROM ".$this->table("sign_record")." WHERE wxid=$wxid";
		$id = $this->getOne($sql);
		if(!$id){
			$info['wxid'] = $wxid;
			$info['signnum'] = 0;
			$info['totalnum'] = 0;
			$this->insert($info, "sign_record");
		}
	}
	
	public function sign($data,$maxSignED=2,$maxSign=15,$spacing=3600){
		$result=array('error'=>0,'content'=>'签到失败',"isFirst"=>false);
		$time = $_SERVER['REQUEST_TIME'];
		$sql = "SELECT lastTime FROM ".$this->table('sign_record')." WHERE wxid=".$data['wxid']." LIMIT 1";
		$lastTime = $this->getOne($sql);
		if($lastTime){//有需要判断
			$last = strtotime(date('Y-m-d 00:00:00',$time));//当前日期最早时候
			if($lastTime>=$last){//今天
				
			}else{//昨天
				$sql=" UPDATE ".$this->table('sign_record')." SET signnum=0 WHERE wxid=".$data['wxid']." LIMIT 1";
				$res = $this->query($sql);
			}
			//
			$sql ="SELECT * FROM ".$this->table("sign_record")." WHERE wxid = ".$data['wxid']." LIMIT 1";
			$record = $this->getRow($sql);
			if($record['totalnum']>=$maxSign){
				$result=array('error'=>0,'content'=>'最多签到次数为15次，感谢您的参与！');
				return $result;
			}
			if($record['signnum']>=$maxSignED){
				$result=array('error'=>0,'content'=>'一天只可以签到2次，明天继续加油！');
				return $result;
			}
			if($time-$record['lastTime']<$spacing){
				$result=array('error'=>0,'content'=>'一个小时内不能连续签到2次，请稍后继续！');
				return $result;
			}
			$sql = "SELECT signnum FROM ".$this->table("sign_record")." WHERE wxid=".$data['wxid']." LIMIT 1";
			$signnum = $this->getOne($sql);
			if($signnum==0){
				$result['isFirst'] = true;
			}
			$sql=" UPDATE ".$this->table('sign_record')." SET signnum=signnum+1,totalnum=totalnum+1,lastTime=".$time." WHERE wxid=".$data['wxid']." LIMIT 1";
			$res = $this->query($sql);
			
		}else{//无，无需判断
			$sql = "SELECT signnum FROM ".$this->table("sign_record")." WHERE wxid=".$data['wxid']." LIMIT 1";
			$signnum = $this->getOne($sql);
			if($signnum==0){
				$result['isFirst'] = true;
			}
			$sql=" UPDATE ".$this->table('sign_record')." SET signnum=signnum+1,totalnum=totalnum+1,lastTime=".$time." WHERE wxid=".$data['wxid']." LIMIT 1";
			$res = $this->query($sql);
		}
		if($res){
			$sql ="SELECT totalnum FROM ".$this->table("sign_record")." WHERE wxid = ".$data['wxid']." LIMIT 1";
			$totalnum = $this->getOne($sql);
			$result = $this->get_msg($totalnum,$result['isFirst']);
		}
		return $result;
	}
	public function get_msg($totalnum,$isFirst){
		$result=array('error'=>1,'content'=>'送你一份礼物，请在今日签到礼物处查收！',"show"=>1,"id"=>"","isFirst"=>$isFirst);
		$sql ="SELECT id FROM ".$this->table('sign_persent')." WHERE number = $totalnum ";
		$persent_id = $this->getOne($sql);
		if($persent_id){//有值
			$sql =" SELECT * FROM ".$this->table("sign_persent")." WHERE id=".$persent_id." LIMIT 1";
			$row = $this->getRow($sql);
			$result['id'] = $row['id'];
			return $result;
		}else{//无值
			$sql =" SELECT * FROM ".$this->table("sign_persent")." WHERE number>".$totalnum." ORDER BY number LIMIT 1";
			$row = $this->getRow($sql);
			$content = "签到".$row['number']."次送".$row['name']."哦，送完即止！";
			$result['content'] = $content;
			$result['show'] = 0;
			return $result;
		}
	}
	public function get_sign_record($wxid){
		$sql = "SELECT * FROM ".$this->table("sign_record")." WHERE wxid='".$wxid."' LIMIT 1";
		$result = $this->getRow($sql);
		return $result;
	}
	public function get_isSign_isPersent($wxid,$maxSignED=2,$maxSign=15,$spacing=3600){
		$result=array('isSign'=>1,'isPersent'=>0);
		$time = $_SERVER['REQUEST_TIME'];
		$sql = "SELECT lastTime FROM ".$this->table('sign_record')." WHERE wxid=".$wxid." LIMIT 1";
		$lastTime = $this->getOne($sql);
		if($lastTime){//有需要判断
			$last = strtotime(date('Y-m-d 00:00:00',$time));//当前日期最早时候
			if($lastTime>=$last){//今天
				$result['isSign'] = 1;
				$sql ="SELECT * FROM ".$this->table("sign_record")." WHERE wxid = ".$wxid." LIMIT 1";
				$record = $this->getRow($sql);
				if($record['signnum']>=$maxSignED){
					$result['isSign'] = 0;
				}
				if($record['totalnum']>=$maxSign){
					$result['isSign'] = 0;
				}
				if($time-$record['lastTime']<$spacing){
					$result['isSign'] = 0;
				}
			}else{//昨天
				$result['isSign'] = 1;
				$sql ="SELECT * FROM ".$this->table("sign_record")." WHERE wxid = ".$wxid." LIMIT 1";
				$record = $this->getRow($sql);
				if($record['totalnum']>=$maxSign){
					$result['isSign'] = 0;
				}
			}
			$sql ="SELECT id FROM ".$this->table('sign_persent')." WHERE number <= ".$record['totalnum']." ORDER BY number ";
			$persents = $this->getAll($sql);
			foreach ($persents as $k=>$v){
				$sql ="SELECT id FROM ".$this->table("sign_persent_record")." WHERE persent_id = ".$v['id']." AND wxid=".$wxid." LIMIT 1";
				$isP = $this->getOne($sql);
				if(!$isP){
					$result['isPersent'] = 1;
					$sql = "SELECT * FROM ".$this->table('sign_persent')." WHERE id = ".$v['id'];
					$persent = $this->getRow($sql);
					$result['persent'] = $persent;//最接近的礼物
				}
			}
		}else{//无，无需判断
			$result['isSign'] = 1;
		}
		return $result;
	}
	
	public function persent($wxid,$persent_id){
		$result =array("error"=>0,"content"=>"该奖品您已领取");
		$sql="SELECT id FROM ".$this->table("sign_persent_record")." WHERE persent_id = ".$persent_id." AND wxid=".$wxid." LIMIT 1";
		$id = $this->getOne($sql);
		if(!$id){
			$info['wxid']= $wxid;
			$info['persent_id'] = $persent_id;
			$info['addtime'] = $_SERVER['REQUEST_TIME'];
			$res = $this->insert($info, "sign_persent_record");
			if($res){
				$result['error'] = 1;
				$result['content'] = "成功领取该奖品";
				$sql=" SELECT url FROM ".$this->table("sign_persent")." WHERE id=".$persent_id." LIMIT 1";
				$result['url'] = $this->getOne($sql);
			}
		}
		return $result;
	}
}